//
//  HyqModifyController.swift
//  约麻
//
//  Created by rimi on 2017/7/19.
//  Copyright © 2017年 Sweet. All rights reserved.
//

import UIKit
import AVOSCloud
import Hyphenate

class HyqModifyController: UIViewController {
    var window: UIWindow?
    @IBOutlet weak var nameM: UITextField!
    override func viewDidLoad() {
        super.viewDidLoad()
        navigationController?.isNavigationBarHidden = false
        let name = AVUser.current()?.object(forKey: "name")
        self.nameM.text = name as? String
        
      
        setnavigation()
        // Do any additional setup after loading the view.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    //设置导航栏
    func setnavigation(){
        navigationItem.title = "昵称"
        navigationController?.navigationBar.barTintColor = #colorLiteral(red: 1, green: 1, blue: 1, alpha: 1)
        //定义标题颜色与字体大小字典
        let dict:NSDictionary = [NSForegroundColorAttributeName: #colorLiteral(red: 0, green: 0, blue: 0, alpha: 1),NSFontAttributeName : UIFont.boldSystemFont(ofSize: 15)]
        //标题设置颜色与字体大小
        navigationController?.navigationBar.titleTextAttributes = dict as? [String : AnyObject]
        
        let Btn = UIButton(frame: CGRect(x: 0, y: 0, width: 15, height: 15))
        Btn.setImage(UIImage.init(named:"黑色返回"), for: .normal)
        Btn.addTarget(self, action: #selector(backClicked), for: .touchUpInside)
        let back = UIBarButtonItem(customView: Btn)
        navigationItem.leftBarButtonItem = back
        let register = UIBarButtonItem(title: "保存", style: .plain, target: self, action: #selector(registerC))
        register.setTitleTextAttributes([NSForegroundColorAttributeName:UIColor.black,NSFontAttributeName:UIFont.boldSystemFont(ofSize: 13)], for: .normal)
        
        navigationItem.rightBarButtonItem = register
    }
    // 保存
    func registerC(){
        AVUser.current()?.setObject(nameM.text, forKey: "name")
        AVUser.current()?.save()
        ZWHAlertController.showAlert(vc: self, title: "保存成功", message: "", style: .alert)
      
    }
    
    func backClicked() {
        self.dismiss(animated: true, completion: nil)
    }
    // 退出登录
    @IBAction func logOutBtn(_ sender: UIButton) {
        AVUser.logOut()
        let tabbarVC = HyqTabBarController()
        let error = EMClient.shared().logout(true)
        if error == nil{
            let nva = UINavigationController(rootViewController: tabbarVC)
            UIApplication.shared.keyWindow?.rootViewController = nva
        }
    }
    
}
